package com.example.demo.practice.monitoring;

import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Switch;


import com.example.demo.practice.R;
import com.example.demo.practice.ui.TitleView;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;

import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;

import com.github.mikephil.charting.formatter.IFillFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.listener.ChartTouchListener;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.MPPointF;

import java.util.ArrayList;

public class Sensor extends AppCompatActivity {
    private PieChart chart1;
    private PieChart chart2;
    private PieChart chart3;
    private LineChart lineChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        setContentView(R.layout.activity_sensor);
        initChart();
    }

    private void initChart() {
        //禁止点击checkbox
        CheckBox checkBox1= (CheckBox) findViewById(R.id.check_normal);
        CheckBox checkBox2= (CheckBox) findViewById(R.id.check_fault);
        CheckBox checkBox3= (CheckBox) findViewById(R.id.check_alarm);
        checkBox1.setClickable(false);
        checkBox2.setClickable(false);
        checkBox3.setClickable(false);

        chart1 = (PieChart) findViewById(R.id.chart1);
        chart2 = (PieChart) findViewById(R.id.chart2);
        chart3 = (PieChart) findViewById(R.id.chart3);
        initPieChart(chart1);
        initPieChart(chart2);
        initPieChart(chart3);
        lineChart = (LineChart) findViewById(R.id.lineChart);
        initLineChart(lineChart);

        setData(0.25f,chart2);
        setData(0.15f,chart3);
    }

    /**
     * 初始化折线图
     *
     * @param mChart
     */
    private void initLineChart(LineChart mChart) {
        mChart.setViewPortOffsets(0, 0, 0, 0);
        mChart.setBackgroundColor(getResources().getColor(R.color.windowBackground));

        // no description text
        mChart.getDescription().setEnabled(false);

        // enable touch gestures
        mChart.setTouchEnabled(true);

        // enable scaling and dragging
        mChart.setDragEnabled(true);
        mChart.setScaleEnabled(true);

        // if disabled, scaling can be done on x- and y-axis separately
        mChart.setPinchZoom(false);

        mChart.setDrawGridBackground(false);
        mChart.setMaxHighlightDistance(300);

        XAxis x = mChart.getXAxis();
        x.setEnabled(false);

        YAxis y = mChart.getAxisLeft();
        y.setTypeface(Typeface.SERIF);
        y.setLabelCount(6, false);
        y.setTextColor(Color.WHITE);
        y.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
        y.setDrawGridLines(false);
        y.setAxisLineColor(Color.WHITE);

        mChart.getAxisRight().setEnabled(false);

        // add data
        setLineData(20, 50, mChart);

        mChart.getLegend().setEnabled(false);

        mChart.animateXY(2000, 2000);
        mChart.invalidate();

    }

    private void setLineData(int count, int range, LineChart mChart) {
        ArrayList<Entry> yVals = new ArrayList<Entry>();

        for (int i = 0; i < count; i++) {
            float mult = (range + 1);
            float val = (float) (Math.random() * mult) + 20;
            yVals.add(new Entry(i, val));
        }

        LineDataSet set1;

        if (mChart.getData() != null &&
                mChart.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) mChart.getData().getDataSetByIndex(0);
            set1.setValues(yVals);
            mChart.getData().notifyDataChanged();
            mChart.notifyDataSetChanged();
        } else {
            // create a dataset and give it a type
            set1 = new LineDataSet(yVals, "DataSet 1");

            set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
            set1.setCubicIntensity(0.2f);
            set1.setDrawCircles(false);
            set1.setLineWidth(1.8f);
            set1.setCircleRadius(4f);
            set1.setCircleColor(Color.WHITE);
            set1.setHighLightColor(Color.rgb(244, 117, 117));
            set1.setColor(getResources().getColor(R.color.lineChart_fill));
            set1.setFillColor(getResources().getColor(R.color.lineChart_fill));
            set1.setFillAlpha(255);
            set1.setDrawFilled(true);
            set1.setDrawHorizontalHighlightIndicator(false);
            set1.setFillFormatter(new IFillFormatter() {
                @Override
                public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
                    return -10;
                }
            });

            // create a data object with the datasets
            LineData data = new LineData(set1);
            data.setValueTypeface(Typeface.SERIF);
            data.setValueTextSize(9f);
            data.setDrawValues(false);

            // set data
            mChart.setData(data);
        }

    }

    private void initPieChart(PieChart mChart) {
        mChart.getLegend().setEnabled(false);
        mChart.setUsePercentValues(true);
        mChart.setDragDecelerationFrictionCoef(0.95f);

        mChart.setCenterTextTypeface(Typeface.SANS_SERIF);
        mChart.setCenterTextColor(Color.WHITE);
        mChart.setDrawHoleEnabled(true);
        mChart.setHoleColor(getResources().getColor(R.color.pieChart_center));

        mChart.setTransparentCircleColor(Color.WHITE);
        mChart.setTransparentCircleAlpha(110);

        mChart.setHoleRadius(59f);
        mChart.setTransparentCircleRadius(61f);

        mChart.setDrawCenterText(true);
        mChart.setRotationAngle(0);
        // enable rotation of the chart by touch
        mChart.setRotationEnabled(true);
        mChart.setHighlightPerTapEnabled(true);
        Description description = new Description();
        description.setText("");
        mChart.setDescription(description);
        //mChart.setOnChartValueSelectedListener(this);
        mChart.setOnTouchListener(new Listener(mChart));
        setData(0.6f, mChart);
        mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);


    }

    /**
     * 设置饼图数据
     *
     * @param range
     * @param mChart
     */
    private void setData(float range, PieChart mChart) {
        int centerText = (int) (range * 100);
        mChart.setCenterText(centerText + "%");

        ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
        PieEntry pieEntry1 = new PieEntry(range);
        PieEntry pieEntry2 = new PieEntry(1f - range);
        entries.add(pieEntry1);
        entries.add(pieEntry2);


        PieDataSet dataSet = new PieDataSet(entries, "1");

        dataSet.setDrawIcons(false);
        dataSet.setSliceSpace(0);
        dataSet.setIconsOffset(new MPPointF(0, 40));
        dataSet.setSelectionShift(5f);

        // add a lot of colors

        ArrayList<Integer> colors = new ArrayList<Integer>();

        colors.add(getResources().getColor(R.color.pieChart_con1));
        colors.add(getResources().getColor(R.color.pieChart_con2));
        dataSet.setColors(colors);
        //dataSet.setSelectionShift(0f);

        PieData data = new PieData(dataSet);
        data.setDrawValues(false);
        mChart.setData(data);


        // undo all highlights
        mChart.highlightValues(null);

        mChart.invalidate();
    }

    private class Listener extends ChartTouchListener {

        public Listener(Chart chart) {
            super(chart);
        }

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            CheckBox checkBox1= (CheckBox) findViewById(R.id.check_normal);
            CheckBox checkBox2= (CheckBox) findViewById(R.id.check_fault);
            CheckBox checkBox3= (CheckBox) findViewById(R.id.check_alarm);
            checkBox1.setChecked(false);
            checkBox2.setChecked(false);
            checkBox3.setChecked(false);
            switch (view.getId()){
                case R.id.chart1:
                    checkBox1.setChecked(true);
                    break;
                case R.id.chart2:
                    checkBox2.setChecked(true);
                    break;
                case R.id.chart3:
                    checkBox3.setChecked(true);
                    break;
                default:break;
            }
            setLineData(20, 50, lineChart);
            lineChart.invalidate();
            lineChart.animateXY(1000, 1000);
            return false;
        }
    }
}
